#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
#define N ((1<<17)+5)
int a[N],A[N];
int n,m,Tc;
int c[N];
char tmp[N];
int D[N],X[4];
ll diff[N];
int K,L;
int qd;
int tc[N<<1],fc[N<<1];
void dfs1(int x,int dep){
    if(dep>=K){
        tc[x]=x-(1<<K)+1;
        fc[x]=a[x-(1<<K)+1];
        return;
    }
    dfs1(x<<1,dep+1);
    dfs1(x<<1|1,dep+1);
    if(!D[x]){
        if(fc[x<<1]>=K-dep)tc[x]=tc[x<<1],fc[x]=fc[x<<1];
        else tc[x]=tc[x<<1|1],fc[x]=fc[x<<1|1];
    }
    else{
        if(fc[x<<1|1]>=K-dep)tc[x]=tc[x<<1|1],fc[x]=fc[x<<1|1];
        else tc[x]=tc[x<<1|1],fc[x]=fc[x<<1];
    }
}
void dfs2(int x,int hi,int h,int L,int R){
    // printf("dfs2 %d %d %d %d %d\n",x,hi,h,L,R);
    if(!h){
        int val=x-(1<<K)+1;
        if(val>=L && L<min(val,R)){
            diff[L]+=val,diff[min(val,R)]-=val;
            // printf("addvir %d %d\n",L,val);
        }
        if(a[val]>=hi && max(L,val)<R){
            diff[max(L,val)]+=val,diff[R]-=val;
        }
        return;
    }
    if(!D[x]){
        dfs2(x<<1,max(hi,h),h-1,L,R);
        dfs2(x<<1|1,hi,h-1,L,fc[x<<1]<h?R:min(R,tc[x<<1]));
    }
    else{
        dfs2(x<<1,hi,h-1,L,fc[x<<1|1]>=h?min(R,tc[x<<1|1]):R);
        dfs2(x<<1|1,max(hi,h),h-1,L,R);
    }
}
int main(){
    freopen("arena.in","r",stdin);
    freopen("arena.out","w",stdout);
    scanf("%d%d",&n,&m);
    for(int i=1;i<=n;i++)scanf("%d",&A[i]);
    K=__lg(n-1)+1;
    for(int i=1;i<=m;i++)scanf("%d",&c[i]);
    for(int d=K-1;d>=0;d--){
        scanf("%s",tmp);
        for(int i=0;i<(1<<d);i++)D[i+(1<<d)]=tmp[i]-'0';
    }
    scanf("%d",&Tc);
    while(Tc--){
        for(int i=0;i<4;i++)scanf("%d",&X[i]);
        for(int i=1;i<=n;i++)a[i]=A[i]^X[i&3];
        // for(int i=1;i<=n;i++){
        //     if(a[i]<=500)printf("%d ",a[i]);
        //     else printf("M ");
        // }
        // printf("\n");
        ll ans=0;
        dfs1(1,0);
        // for(int i=1;i<=15;i++)printf("tc[%d]=%d,fc[%d]=%d\n",i,tc[i],i,fc[i]);
        for(int i=0;i<=(1<<K);i++)diff[i]=0;
        for(int d=0;d<=K;d++){
            dfs2(1<<(K-d),0,d,(d==0?1:(1<<d-1)+1),(1<<d)+1);
        }
        for(int i=1;i<=n;i++)diff[i]+=diff[i-1];
        // for(int i=1;i<=n;i++)printf("%lld ",diff[i]);
        // printf("\n");
        for(int i=1;i<=m;i++)ans^=i*diff[c[i]];
        printf("%lld\n",ans);
    }
    return 0;
}